// pages/order-list/order-list.js
const init = require('../../utils/init')
const api = require('../../utils/api')
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    maplist: [],
    cardinfo: [{
      name: "姓名",
      context: "Jevon"
    }, {
      name: "订单编号",
      context: "WX7534445"
    }],
    currentTab: 0,
    tabs: [{
      name: "全部"
    }, {
      name: "待付款"
    }, {
      name: "待使用"
    }, {
      name: "使用中"
    }, {
      name: "已完成",
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.index){
      this.setData({
        currentTab: options.index
      })
    }
    init.initComponents(this)

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.change()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  test() {
    console.log("test")
  },

  change(e) {
    console.log("进入订单页")
    this.setData({
      currentTab: e ? e.detail.index : this.data.currentTab
    })
    var index = e ? e.detail.index : this.data.currentTab;
    if (!app.globalData.openid) {
      console.log("进入 getWxUserInfoCallback")
      app.getWxUserInfoCallback = (openid) => {
        console.log(openid)
        this.showTab(index,openid)
      }
    } else {
      console.log("进入 getOrderInfoToShow")
      this.showTab(index,app.globalData.openid)
    }
  },


  showTab(index,openid) {
    var that = this
    var dto = {
      openId: openid,
      orderStatus: ''
    }
    console.log(index,dto)
    if (index == 0) {
      api.getOrderInfoToShow(dto).then(res => {
        console.log(res)
        that.setData({
          maplist: res.info
        })
      })
    }

    if (index == 1) {
      console.log("进入到index===1")
      dto.orderStatus = 'on'
      api.getOrderInfoToShow(dto).then(res => {
        console.log(res)
        that.setData({
          maplist: res.info
        })
      })
    }

    if (index == 2) {
      dto.orderStatus = 'pay'
      api.getOrderInfoToShow(dto).then(res => {
        console.log(res)
        that.setData({
          maplist: res.info
        })
      })
    }

    if (index == 3) {
      dto.orderStatus = 'use'
      api.getOrderInfoToShow(dto).then(res => {
        that.setData({
          maplist: res.info
        })
      })
    }

    if (index == 4) {
      dto.orderStatus = 'off'
      api.getOrderInfoToShow(dto).then(res => {
        console.log(res)
        that.setData({
          maplist: res.info
        })
      })
    }
  }
})